﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace blackhouse.WebNemo.Mvc.Infrastructure {
    public class SizeModelBinder : IModelBinder {

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
            var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            if (valueResult == null || String.IsNullOrEmpty(valueResult.AttemptedValue))
                return Size.Empty;
            var sizeParts = valueResult.AttemptedValue.Split('x');
            int width, height;
            if (Int32.TryParse(sizeParts[0], out width)
                && Int32.TryParse(sizeParts[1], out height))
                return new Size(width, height);
            return Size.Empty;
        }

    }
}